const date = new Date()
const years = []
const months = []
const days = []

for (let i = 2018; i <= date.getFullYear(); i++) {
  years.push(i)
}

for (let i = 1; i <= 12; i++) {
  months.push(i)
}

for (let i = 1; i <= 31; i++) {
  days.push(i)
}

var app = getApp();
var maxPageIndex;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    ifShownoData:true,
    ifscroll:false,
    wrapDate:false,
    wrapCategory: false,
    animationData: {},
    cStatus:false,
    dStatus: false,
    oStatus: false,
    categoryName:"全部科目",
    listCategory:[

      {
        "name": "全部科目",
        "id": ""
      },
      {
        "name": "支出",
        "id": 1
      },
      {
        "name": "收入",
        "id": 2
      },

    ],
    orderName:"订单来源",
    orderSelect:"",
    listOrder: [

      {
        "name": "订单来源",
        "id": ""
      },
      {
        "name": "普通订单",
        "id": 1
      },
      {
        "name": "企业订单",
        "id": 2
      },

    ],
    categorySelect:"",
    years: years,
    year: date.getFullYear(),
    months: months,
    month: date.getMonth() + 1,
    days: days,
    day: 2,
    value: [9999, date.getMonth(), 1],
    wxType: "",
    monthStatus:true,
    pageIndex: 1,
    transactionVo: "",
    income:"",
    expenditure: ""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    that.setData({ wxType: app.globalData.wxType });
    that.statement();
    that.toTranSumCount();
  },

  //显示弹窗事件
  wrapCategorySelect: function (e) {
    var that = this;
    that.data.indexNumber = e.currentTarget.dataset.index;

    // 创建一个动画实例
    var animation = wx.createAnimation({
      // 动画持续时间
      duration: 300,
      // 定义动画效果，当前是匀速
      timingFunction: 'linear'
    })
    // 将该变量赋值给当前动画
    that.animation = animation
    // 先在y轴偏移，然后用step()完成一个动画
    animation.translateY(-200).step()
    // 用setData改变当前动画
    that.setData({
      // 通过export()方法导出数据
      animationData: animation.export(),
      // 改变view里面的Wx：if
      //wrapCategory: true,
      ifscroll: true,
      wrapCategory: !that.data.wrapCategory,
      cStatus: !that.data.cStatus,
      wrapDate:false,
      dStatus: false,
      wrapOrder: false,
      oStatus: false,
      
    })
    // 设置setTimeout来改变y轴偏移量，实现有感觉的滑动
    setTimeout(function () {
      animation.translateY(0).step()
      that.setData({
        animationData: animation.export(),
        
      })
    }, 0)
  },
  //隐藏弹窗事件
  hideModal: function (e) {
    var that = this;
    var animation = wx.createAnimation({
      duration: 1000,
      timingFunction: 'linear'
    })
    that.animation = animation
    animation.translateY(-200).step()
    that.setData({
      animationData: animation.export()

    })
    setTimeout(function () {
      animation.translateY(0).step()
      that.setData({
        ifscroll: false,
        animationData: animation.export(),
        wrapCategory: false,
        cStatus:false,
        wrapDate: false,
        dStatus: false,
        wrapOrder: false,
        oStatus: false
      })
    }, 100)

  },

//categoryList//收入支出选择
  categoryList: function (e) {
    var that = this;
    that.setData({//把选中值放入判断值
      categorySelect: e.currentTarget.dataset.select,
      categoryName: e.currentTarget.dataset.text
    })
    that.statement();
    that.toTranSumCount();
    that.hideModal();
  },

  //日期展示
  wrapDateSelect:function(){
    var that = this;
    var animation = wx.createAnimation({
      duration: 300,
      timingFunction: 'linear'
    })
    that.animation = animation
    animation.translateY(-200).step()
    that.setData({
      ifscroll: true,
      animationData: animation.export(),
      wrapDate: !that.data.wrapDate,
      dStatus: !that.data.dStatus,
      wrapCategory: false,
      cStatus: false,
      wrapOrder: false,
      oStatus: false,
      monthStatus:false

    })
    setTimeout(function () {
      animation.translateY(0).step()
      that.setData({
        animationData: animation.export(),

      })
    }, 0)
    that.statement();
    that.toTranSumCount();

  },
  //日期类别选择
  bindChange: function (e) {
    var that = this;
    const val = e.detail.value;
    var year = that.data.years[val[0]];
    var month = that.data.months[val[1]];
    if (year == date.getFullYear() && month == date.getMonth() + 1){
      that.setData({ monthStatus: true});
    }
    that.setData({
      year: that.data.years[val[0]],
      month: that.data.months[val[1]],
      value: [that.data.years[val[0]]-2018, that.data.months[val[1]]-1, 1],
    //  day: this.data.days[val[2]]
    })
    
  },


  //日期确定事件
  sureDateBtn: function () {
    var that = this;
    that.statement();
    that.toTranSumCount();
    that.hideModal();
  },

//订单来源展示
  wrapOrderSelect: function(){
    var that = this;
    var animation = wx.createAnimation({
      duration: 300,
      timingFunction: 'linear'
    })
    that.animation = animation
    animation.translateY(-200).step()
    that.setData({
      ifscroll: true,
      animationData: animation.export(),
      wrapOrder: !that.data.wrapOrder,
      oStatus: !that.data.oStatus,
      wrapCategory: false,
      cStatus: false,
      wrapDate: false,
      dStatus: false

    })
    // 设置setTimeout来改变y轴偏移量，实现有感觉的滑动
    setTimeout(function () {
      animation.translateY(0).step()
      that.setData({
        animationData: animation.export(),

      })
    }, 0)
   
   
  },
  //orderList//订单来源类型选择
  orderList: function (e) {
    var that = this;
    that.setData({//把选中值放入判断值
      orderSelect: e.currentTarget.dataset.select,
      orderName: e.currentTarget.dataset.text
    })
    that.statement();
    that.toTranSumCount();
    that.hideModal();
  },
  //对账单信息
  statement: function(){
    var that = this;
    var arr = new Array;
    wx.showLoading({
      title: '拼命加载中',
      mask: true
    });
    var dateTime = that.data.year + '-' + that.data.month ;
    var tranType = that.data.categorySelect;//收入支出
    var Type = that.data.orderSelect;//订单来源
    wx.request({
      method: "POST",
      url: 'https://www.zaishengwang.com/wx/toTranList.do',
      dataType: 'json',
      header: {
        'content-type': 'application/x-www-form-urlencoded',
      },
      data: {
        userId: app.globalData.userId,
        dateTime: dateTime,
        tranType: tranType,
        type: Type,
        page: that.data.pageIndex,
        size:10
      },
      success: function (res) {
        wx.hideLoading();
        maxPageIndex = res.data.pages.total / 10;
        if (res.data.transactionVo.length > 0) {
          for (var i = 0; i < res.data.transactionVo.length; i++) {
            let createTime = res.data.transactionVo[i].createTime.substring(0, 16);
            res.data.transactionVo[i].createTime = createTime;
          }
          arr = res.data.transactionVo;
          that.setData({ ifShownoData: true });
        } else {
          that.setData({ ifShownoData: false });
        }

        if (that.data.pageIndex != 1) {
          var array = that.data.transactionVo;
          array = array.concat(arr);
          that.setData({ ifShownoData: true, transactionVo: array });
        } else {
          that.setData({ transactionVo: arr });
        }
      },
      fail: function (error) {
        console.log(error);
      }

    })
  },
  //收入支出信息
  toTranSumCount: function () {
    var that = this;
    var dateTime = that.data.year + '-' + that.data.month;
    var tranType = that.data.categorySelect;//收入支出
    var Type = that.data.orderSelect;//订单来源
    wx.request({
      method: "POST",
      url: 'https://www.zaishengwang.com/wx/toTranSumCount.do',
      dataType: 'json',
      header: {
        'content-type': 'application/x-www-form-urlencoded',
      },
      data: {
        userId: app.globalData.userId,
        dateTime: dateTime,
        tranType: tranType,
        type: Type,
      },
      success: function (res) {
        if (tranType==1){
          that.setData({ expenditure: res.data.transactionVo.expenditure })
        } else if (tranType == 2){
          that.setData({ income: res.data.transactionVo.income })
        }else{
          that.setData({ income: res.data.transactionVo.income, expenditure: res.data.transactionVo.expenditure, })
        }
       
      },
      fail: function (error) {
        console.log(error);
      }

    })
  },

  //对账单详情页
  goStatementDetail: function (e) {
    var bId = e.currentTarget.dataset.bid;
    wx.navigateTo({
      url: 'statement_detail?bId=' + bId,
    })
  },

  noScroll: function () {
    //微信小程序有bind和catch两种事件绑定方法，bind事件绑定不会阻止冒泡事件向上冒泡，catch事件绑定可以阻止冒泡事件向上冒泡。我们使用catch绑定手指触摸事件，然后他的父节点就不会收到事件冒泡，这样就实现了禁止页面上下滑动
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
    * 页面相关事件处理函数--监听用户下拉动作
    */
  onPullDownRefresh: function () {
    var that = this;
    that.setData({
      pageIndex: 1, // 每次触发下拉事件pageIndex=1
      transactionVo: "",
    });
    that.statement();
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this;
    if (maxPageIndex < (that.data.pageIndex)) {
      return;
    }
    that.setData({
      pageIndex: that.data.pageIndex + 1,
    })
    // 上拉获取更多数据
    that.statement();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  }
})